home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_03 / thomas / predict.cpp < prev   
C/C++ Source or Header  |  1994-01-27  |  4KB  |  152 lines

  1. enum ReturnValue
  2.  {
  3.     Failure,
  4.     Success
  5.  };
  6.  
  7. const DEF_CARDINAL=500;         //number of commands a_matrix can 
  8. hold
  9. const DEF_DELTA_CARDINAL=100;   //increment of cardinal_a_matrix
  10. const MAX_SIZE_COMMAND=50;      //maximum size of command
  11.  
  12. class predictor
  13.  {
  14.         int num_commands;       //current number of unique commands
  15.         int cardinal_a_matrix;  //max number of commands a_matrix 
  16. can hold
  17.         int *a_matrix;          //pointer to the adjacency matrix
  18.         char *commands;         //list of unique commands
  19.         int last_ordinal;       //index of last command
  20.  
  21.         ReturnValue size(int cardinality);  //malloc or remalloc to 
  22. create
  23.                                             // approp. a_matrix & 
  24. commands
  25.         int is_unique(char *command);       //checks commands[] for 
  26. command
  27.                                             // <0 =>No,else=>index 
  28. of command
  29.         ReturnValue insert(char *command);  //add command to commands
  30.         ReturnValue update(int ordinal);    //increment a_matrix entry
  31.  
  32.    public:
  33.         predictor()
  34.          {
  35.             num_commands=0;
  36.             cardinal_a_matrix=DEF_CARDINAL;
  37.             size(cardinal_a_matrix);
  38.             last_ordinal=-1;
  39.          }
  40.         predictor(int cardinality) //overload constructor
  41.          {
  42.             num_commands=0;
  43.             cardinal_a_matrix=cardinality;
  44.             size(cardinal_a_matrix);
  45.             last_ordinal=-1;
  46.          }
  47.         ~predictor()            //destructor
  48.          {
  49.             cardinal_a_matrix=0;
  50.             free(a_matrix);
  51.             free(commands);
  52.          }
  53.  
  54.         ReturnValue PutNextCommand(char *);
  55.         ReturnValue GetNextCommand(char *);
  56.         int WhenNextCommand(char *);
  57.  };
  58.  
  59.  
  60. ReturnValue predictor::PutNextCommand(char *command)
  61.  {
  62.     int ordinal;
  63.  
  64.     if(num_commands+1 >= cardinal_a_matrix) //no space ?
  65.  
  66.      {
  67.         cardinal_a_matrix+=DEF_DELTA_CARDINAL;
  68.         if(size(cardinal_a_matrix)==Failure)//can not allocate space
  69.             return(Failure);
  70.      }
  71.  
  72.     if((ordinal=is_unique(command))==-1)    //command encountered 
  73. before?
  74.      {
  75.         if(insert(command) == Failure)      //no - add it
  76.             return(Failure);
  77.         ordinal=num_commands;               //new ordinal number of 
  78. command
  79.      }
  80.  
  81.     return(update(ordinal));                //return appropriate value
  82.  }
  83.  
  84.  
  85. ReturnValue predictor::GetNextCommand(char *command)
  86.  {
  87.     int ordinal,max=0,i;
  88.     int *last_command;
  89.  
  90.     //note: assumes a_matrix is stored in row major fashion
  91.  
  92.     last_command=a_matrix+                  //calculate row of last_command
  93.             last_ordinal*
  94.             cardinal_a_matrix*sizeof(int);  //size of a row
  95.     //Note: With certain compilers, sizeof(int) is unnecessary
  96.  
  97.     for(i=0; i<num_commands; ++i)           //do MAX() function
  98.      {
  99.         if(*(last_command+i*sizeof(int))>max)
  100.          {
  101.             max=*(last_command+i*sizeof(int));
  102.             ordinal=i;
  103.          }
  104.      }
  105.  
  106.     if(max>0)
  107.      {
  108.         strcpy(command,commands*MAX_SIZE_COMMAND);
  109.         return(Success);
  110.      }
  111.     else
  112.      {
  113.         command=NULL;
  114.         return(Failure);
  115.      }
  116.  }
  117.  
  118. int predictor::WhenNextCommand(char *command)
  119.  {
  120.     int count=1;
  121.     int old_last_ordinal=last_ordinal;  //save state of predictor
  122.     int any_cycle[num_commands];        //check to see if we have 
  123. cycled
  124.     int i;
  125.     char pcommand[MAX_SIZE_COMMAND];
  126.  
  127.     //zero
  128.     for(i=0; i<num_commands; ++i)
  129.  
  130.         any_cycle[i]=0;
  131.  
  132.     while(GetNextCommand(command)==Success)
  133.      {
  134.         if(!strcmp(pcommand,command))   //found the command
  135.             break;
  136.  
  137.         i=is_unique(pcommand);          //find index of predicted 
  138. command
  139.         if(!any_cyle[i]^1)              //test and check
  140.          {
  141.             count=0;                    //we have cycled - failure
  142.             break;
  143.          }
  144.  
  145.         ++count;
  146.      }
  147.     return(count);
  148.  }
  149.  
  150. /* End of File */ 
  151.  
  152.